package com.five.api.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.five.api.dao.ArticleLabelMapper;
import com.five.api.dao.ArticleMapper;
import com.five.api.service.ArticleService;
import com.five.entity.Article;
import com.five.entity.ArticleLabel;
import com.five.entity.Label;
import com.five.entity.User;
import com.five.entity.qo.ArticleQo;
import com.five.entity.qo.UsersAndArticles;
import com.five.qo.ArticleCommentQO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 博客文章表 服务实现类
 * </p>
 *
 * @author star
 * @since 2021-06-16
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleLabelMapper articleLabelMapper;

    @Override
    public List<ArticleCommentQO> selectArticleByUserId(Long userId) {

        List<ArticleCommentQO> articles = baseMapper.selectArticleByUserId(userId);
        return articles;
    }

    @Override
    public List<Article> selectArticleList() {

//        QueryWrapper queryWrapper = new QueryWrapper();
////        List<Article> articles = baseMapper.selectList(queryWrapper);
////        return articles;
        List articleList = articleMapper.selectArticleList();
        return articleList;
    }

    @Override
    public List selectArticleByPage(Long page, ArticleQo articleQo) {
        Page pages = new Page();
        pages.setCurrent(page);
        pages.setSize(5);

        List articles = articleMapper.selectArticleByPageAndType(pages, articleQo);
        return articles;
    }

    @Override
    public UsersAndArticles selectArticleToSum() {
        UsersAndArticles usersAndArticles = articleMapper.selectArticleToSum();

        return usersAndArticles;
    }

    @Override
    public Article getArticleDetail(Long id) {
        Article articleDetail = baseMapper.getArticleDetail(id);
        // 获取文章关联的标签列表
        List<ArticleLabel> articleLabels = articleLabelMapper.selectArticleLabels(articleDetail.getId());
        articleDetail.setArticleLabel(articleLabels);
        return articleDetail;
    }

    @Override
    public int deleteArticle(Long id) {
        int i = baseMapper.deleteById(id);
        return i;
    }

    @Override
    public Long addArticle(Article article, String labels) {


        User user = (User) SecurityUtils.getSubject().getPrincipal();

        article.setUserId(user.getId());

        if (labels != "") {

            String[] labelsArray = labels.split(",");

            //文章添加标签
            for (int i = 0; i < labelsArray.length; i++) {
                articleLabelMapper.addArticleLabel(article.getId(), Long.parseLong(labelsArray[i]));
            }
        }


        //新增文章
        long insertNumber = articleMapper.insert(article);


        return insertNumber;
    }

    @Override
    public int updateArticle(Article article, boolean isAddCount) {
        if (isAddCount) {
            if (article.getBrowseAmount() != null) {
                article.setBrowseAmount(article.getBrowseAmount() + 1);
            } else if (article.getPraiseNumber() != null) {
                article.setPraiseNumber(article.getPraiseNumber() + 1);
            } else {
                article.setCollectionNumber(article.getCollectionNumber() + 1);
            }
        } else {
            article.setCollectionNumber(article.getCollectionNumber() - 1);
        }

        return baseMapper.updateById(article);
    }

    @Override
    public int updateArticleDetail(ArticleQo article , String labels){
        System.out.println("标签："+labels);
        // 删除文章的所有标签
        QueryWrapper<ArticleLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id",article.getId());
        articleLabelMapper.delete(wrapper);

        if (!"".equals(labels)){
            // 添加修改文章后的标签
            String[] labelsArray = labels.split(",");
            for (int i = 0; i < labelsArray.length; i++) {
                articleLabelMapper.addArticleLabel(article.getId(),Long.parseLong(labelsArray[i]));
            }
        }
        return articleMapper.updateArticleDetail(article.getArticleTypeId(),article.getTitle(),article.getArticleDescribe(),article.getContent(),article.getId());
    }
}
